<template>
	<view>
		<u-sticky :enable="stickyEnable">
			<view class="padding-sm bg-white">
				<u-search placeholder="搜索名称" v-model="page.cond.name" @search="search" @custom="search"></u-search>
			</view>
		</u-sticky>
		<view v-for="(item,index) in result.records" :key="index" class="solid-top">
			<tui-swipe-action :actions="actions" :params="item" @click="actionHandler">
				<template #content>
					<view class="padding-sm flex align-center" @click.stop="nav(item)">
						<view class="flex-sub">
							<view class="text-lg text-bold text-cut">{{item.name}}</view>
							<view class="text-gray">{{item.path}}</view>
							<view class="text-gray">{{item.cron}}</view>
						</view>
					</view>
				</template>
			</tui-swipe-action>
		</view>
		<u-loadmore :status="result.current < result.pages?'loading':'nomore'" />
		<coco-fixed-button @click.native="edit"></coco-fixed-button>
	</view>
</template>

<script>
export default {
	onLoad: function(option) {
		if (option.execId) {
			this.execId = option.execId || '';
			this.execName = option.execName || '';
			this.page.cond.execId = option.execId || '';
			this.list();
		}
	},
	onShow: function() {
		this.stickyEnable = true;
	},
	onHide: function() {
		this.stickyEnable = false;
	},
	onReady: function() {},
	onReachBottom: function() {
		if (this.result.current < this.result.pages) {
			this.page.current += 1;
			this.list();
		}
	},
	data() {
		return {
			stickyEnable: false,
			actions: [{
				name: "修改",
				width: 70,
				fontsize: 30,
				color: '#fff',
				background: '#ffaa00'
			}, {
				name: '删除',
				color: '#fff',
				fontsize: 30, //单位rpx
				width: 70, //单位px
				background: '#ff5500'
			}],
			page: {
				current: 1,
				size: 20,
				total: 0,
				orders: [{column: "create_time", asc: false}],
				cond: {execId: null, name: null}
			},
			result: {
				current: 1,
				pages: 1,
				records: []
			},
			execId: null,
			execName: null,
		};
	},
	methods: {
		// 添加
		edit(item) {
			uni.navigateTo({
				url: `job-edit?id=${item.id || ''}&execId=${this.execId}&execName=${this.execName}`,
				events: {
					refresh: () => {
						this.search();
					}
				}
			});
		},
		// 搜索
		search() {
			this.page.current = 1;
			this.list();
		},
		list() {
			this.$http.post("/admin/sys/scheduler/job/list", this.page, res => {
				let result = res.data || this.result;
				if (this.page.current > 1) {
					result.records = this.result.records.concat(result.records);
				}
				this.result = result;
			}, false, false, true);
		},
		// 删除
		remove(item) {
			this.$utils.modal("提示", "是否删除？", true, flag => {
				if (flag) {
					this.$http.post("/admin/sys/scheduler/job/remove", item, res => {
						let records = this.result.records || [];
						records.splice(records.findIndex(obj => obj.id === item.id), 1);
						this.result.records = records;
					}, true, true, true);
				}
			});
		},
		// 滑动菜单事件处理
		actionHandler(obj) {
			// obj.index, obj.item:params
			if (obj.index === 0) {
				this.edit(obj.item);
			} else if (obj.index === 1) {
				this.remove(obj.item);
			}
		},
		// 查看执行日志
		nav(item) {
			uni.navigateTo({
				url: `record-list?jobId=${item.id}`
			})
		},
	}
}
</script>

<style lang="scss" scoped>
</style>
